/*
 * 文章
 */

import { createAPI } from '@/utils/request'
const formOfEmploymentMapping = new Map([
  [1, '已启用'],
  [0, '已禁用']
])

// 文章列表
export const list = data => createAPI('/articles', 'get', data)
  .then(res => ({
    ...res.data,
    items: res.data.items.map(i => ({
      ...i,
      states: formOfEmploymentMapping.get(i.state)
    }))
      .sort((a, b) => a.id - b.id),
    countsInfo: `总共${res.data.counts}条信息`
  }))
  .catch(() => ({}))

// 文章简单列表
export const simple = data => createAPI('/articles/simple', 'get', data)

// 文章详情
export const detail = data => createAPI(`/articles/${data.id}`, 'get', data)

// 文章添加
export const add = data => createAPI('/articles', 'post', data)

// 文章修改
export const update = data => createAPI(`/articles/${data.id}`, 'put', data)

// 文章删除
export const remove = data => createAPI(`/articles/${data.id}`, 'delete', data)

// 文章状态
export const changeState = data => createAPI(`/articles/${data.id}/${data.state}`, 'post', data)
  .then(res => ({
    ...res
  }))
  .catch(() => ({}))
